/**
 * 
 */
package game.kill.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;

import game.kill.mapper.RoomUserMapper;
import game.kill.model.RoomUser;
import game.kill.service.RoomUserService;
import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.Example;

/**
 * @author leiruiqi
 *
 */

@Service("roomUserService")
public class RoomUserServiceImpl implements RoomUserService {

	@Resource
	private RoomUserMapper roomUserMapper;
	
	@Override
	public List<RoomUser> queryRoomUserByRoomId(Long roomId) {
		Example condition = new Example(RoomUser.class);
		condition.createCriteria().andEqualTo("roomId", roomId);
		return roomUserMapper.selectByExample(condition);
	}

	@Override
	public void createPlayer(RoomUser roomUser) {
		roomUserMapper.insert(roomUser);
	}

	@Override
	public RoomUser getByRoomAndUser(Long roomId, Long userId) {
		
		RoomUser roomUser = new RoomUser();
		roomUser.setRoomId(roomId);
		roomUser.setUserId(userId);
		return roomUserMapper.selectOne(roomUser);
	}

	@Override
	public RoomUser getLastRoomUserByUserId(Long userId) {
		Example condition = new Example(RoomUser.class);
		condition.createCriteria().andEqualTo("userId", userId);
		condition.setOrderByClause("id desc");
		PageHelper.startPage(1, 1,false);
		List<RoomUser> roomUserList = roomUserMapper.selectByExample(condition);
		if(roomUserList.size()>0){
			return roomUserList.get(0);
		}
		return null;
	}

	@Override
	public RoomUser getLastRoomUserByUserIdAndStatus(Long userId, Integer status) {
		Example condition = new Example(RoomUser.class);
		condition.createCriteria().andEqualTo("userId", userId).andEqualTo("status", status);
		condition.setOrderByClause("id desc");
		PageHelper.startPage(1, 1,false);
		List<RoomUser> roomUserList = roomUserMapper.selectByExample(condition);
		if(roomUserList.size()>0){
			return roomUserList.get(0);
		}
		return null;
	}

	@Override
	public boolean updatePlayerById(RoomUser roomUser) {
		return 1==roomUserMapper.updateByPrimaryKeySelective(roomUser);
	}

	@Override
	public void removePlayerById(Long id) {
		roomUserMapper.deleteByPrimaryKey(id);
	}

	
	
}
